package br.brpv.silvestrin.vendas.manager;


import android.content.Context;
import br.brpv.silvestrin.vendas.domain.Item;
import br.brpv.silvestrin.vendas.domain.ItemCfg;
import br.brpv.silvestrin.vendas.listeners.ItemsQueryListener;
import br.brpv.silvestrin.vendas.webservice.ServerConnection;
import br.brpv.silvestrin.vendas.webservice.ServerDataCacheService;

import java.util.ArrayList;

public class ItemsManager implements ItemsQueryListener {
    public final static int OK = 0;
    public final static int WAIT_LAST_REQUISITION = -1;

    private Context context;

    ItemsFromServerListener itemFromServerListener;

    private ServerDataCacheService mServerDataCacheBinder;



    private boolean retrieveItemsCfgLock = false;
    private boolean inStock;
    private String selectedItemID;
    private String codPriceTable;
    private int codSelectedCompany;
    private ArrayList<ItemCfg> itemsCfg;


    public ItemsManager(Context context, ItemsFromServerListener itemFromServerListener) {
        this.context = context;
        this.itemFromServerListener = itemFromServerListener;
    }

    public void requestItems(int codSelectedCompany) {
        ArrayList<Item> itemsList = null;
        this.codSelectedCompany = codSelectedCompany;
        if(mServerDataCacheBinder != null) itemsList =  mServerDataCacheBinder.getItemList(codSelectedCompany);
        //if there is no item list for this company in cache get it from server
        if(itemsList == null || itemsList.size() == 0)
            ServerConnection.getItems(codSelectedCompany, this);

        else itemFromServerListener.onItemsRetrieve(itemsList);
    }

    public int requestItemsCfg(String selectedItemID, int codSelectedCompany, boolean inStock, String codPriceTable){
        if(retrieveItemsCfgLock) return WAIT_LAST_REQUISITION;

        retrieveItemsCfgLock = true;
        this.codSelectedCompany = codSelectedCompany;

        this.selectedItemID = selectedItemID;
        this.inStock = inStock;
        this.codPriceTable = codPriceTable;

        ServerConnection.getItemsCfg(codSelectedCompany, selectedItemID, codPriceTable, inStock, this);
        //itemsCfg = new ArrayList<ItemCfg>();
        //ServerConnection.getNoStockSaleItems(codSelectedCompany, selectedItemID, codPriceTable, inStock, this);

        return OK;
    }

    public void requestItemCfgPrice(int codSelectedCompany, String selectedItemID, String codPriceTable , String codItemCfg, String clientId, String estabId) {
        ServerConnection.getItemPrice(codSelectedCompany, selectedItemID, codPriceTable, codItemCfg, clientId, estabId, this);
    }

    @Override
    public void OnWebServiceItemsReturn(ArrayList<Item> items) {
        if(mServerDataCacheBinder != null) mServerDataCacheBinder.addItemListToCompany(codSelectedCompany, items);
        itemFromServerListener.onItemsRetrieve(items);
    }

    @Override
    public void failedOnRetrieveItems() {
        itemFromServerListener.failOnItemsRetrieve();
    }

    @Override
    public void OnWebServiceItemsCfgReturn(ArrayList<ItemCfg> items) {

        if(inStock){
            ArrayList<ItemCfg> newItems = new ArrayList<ItemCfg>();
            for(ItemCfg i: items){
                if(Double.parseDouble(i.getQtdeEstoque()) > 0){
                    newItems.add(i);
                }
            }
            items = newItems;
        }

        itemsCfg = items;

        if(inStock) ServerConnection.getNoStockSaleItems(codSelectedCompany, selectedItemID, codPriceTable, inStock, this);
        else {
            itemFromServerListener.onItemsCfgRetrieve(items);
            retrieveItemsCfgLock = false;
        }
    }

    @Override
    public void failedOnRetrieveItemsCfg() {
        retrieveItemsCfgLock = false;
        itemFromServerListener.failOnItemsCfgRetrieve();
    }

    @Override
    public void OnWebServiceNoStockSaleItemsReturn(ArrayList<ItemCfg> items) {
        for(ItemCfg i : items) {
            removeItemFromArray(i.getCodItem(), itemsCfg);
        }
        itemsCfg.addAll(items);

        itemFromServerListener.onItemsCfgRetrieve(itemsCfg);
        retrieveItemsCfgLock = false;
    }

    @Override
    public void failedOnRetrieveItemsNoStockSale() {
        retrieveItemsCfgLock = false;
//        itemFromServerListener.failOnItemsCfgRetrieve();
        itemFromServerListener.onItemsCfgRetrieve(itemsCfg);
    }

    private void removeItemFromArray(String itemToRemove, ArrayList<ItemCfg> items){
        for(ItemCfg i : items){
            if(i.getCodItem().equals(itemToRemove)){
                items.remove(i);
                break;
            }
        }
    }

    @Override
    public Context getActivity() {
        return context;
    }

    @Override
    public void OnWebServiceItemPriceReturn(String resultString) {
        try{
            double value = Double.parseDouble(resultString);
            itemFromServerListener.onItemCfgPriceRetrieve(value);
        } catch (Exception e){};
    }

    @Override
    public void failedOnItemCfgValue() {
        itemFromServerListener.failOnRetrieveItemCfgPrice();
    }

    public void setmServerDataCacheBinder(ServerDataCacheService mServerDataCacheBinder) {
        this.mServerDataCacheBinder = mServerDataCacheBinder;
    }

    public interface ItemsFromServerListener {
        public void failOnItemsRetrieve();
        public void onItemsRetrieve(ArrayList<Item> items);

        public void failOnItemsCfgRetrieve();
        public void onItemsCfgRetrieve(ArrayList<ItemCfg> items);

        public void failOnRetrieveItemCfgPrice();
        public void onItemCfgPriceRetrieve(double value);

    }

}
